package org.fjsei.yewu.job.config;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.boot.autoconfigure.quartz.QuartzDataSource;
import org.springframework.boot.autoconfigure.quartz.QuartzTransactionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.sql.DataSource;
import java.io.IOException;

@Configuration
@RequiredArgsConstructor
public class QuartzDataSourceConfig {

    @QuartzDataSource
    private final DataSource dataSource;

    @Autowired
    private AutowiringSpringBeanJobFactory  myJobFactory;

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
        //在quartz.properties中的属性被读取并注入后再初始化对象
        propertiesFactoryBean.afterPropertiesSet();
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setQuartzProperties(propertiesFactoryBean.getObject());
        schedulerFactoryBean.setDataSource(dataSource); //Link the Spring boot Datasource to Quartz
        schedulerFactoryBean.setOverwriteExistingJobs(true);
        schedulerFactoryBean.setJobFactory(myJobFactory);   //将job实例化，能够操作进行Spring 注入

        return schedulerFactoryBean;
    }

    //    @Bean
//    public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
//        //获取配置属性
//        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
//        propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
//        //在quartz.properties中的属性被读取并注入后再初始化对象
//        propertiesFactoryBean.afterPropertiesSet();
//        //创建SchedulerFactoryBean
//        SchedulerFactoryBean factory = new SchedulerFactoryBean();
//        factory.setQuartzProperties(propertiesFactoryBean.getObject());
//        //使用数据源，自定义数据源
////        factory.setDataSource(dataSource);
//
//        factory.setQuartzProperties(propertiesFactoryBean.getObject());
//        //https://blog.csdn.net/javaMyGod/article/details/144605838
    ////        factory.setJobFactory(jobFactory); //支持在JOB实例中注入其他的业务对象
//        factory.setApplicationContextSchedulerContextKey("applicationContextKey");
//        factory.setWaitForJobsToCompleteOnShutdown(true);//这样当spring关闭时，会等待所有已经启动的quartz job结束后spring才能完全shutdown。
//        factory.setOverwriteExistingJobs(false);//是否覆盖己存在的Job
//        factory.setStartupDelay(10);//QuartzScheduler 延时启动，应用启动完后 QuartzScheduler 再启动
//
//        return factory;
//    }


/**【关键】不可以添加下面的！！！
* */
//    @QuartzTransactionManager
//    @Bean
//    public DataSourceTransactionManager transactionManager() {
//        return new DataSourceTransactionManager(dataSource);
//    }

}
